import { Body, Controller, ParseIntPipe, Post } from '@nestjs/common';
import { NoticeService } from './notice.service';
import PageDto from './dto/page.dto';
import { DtoPipe } from '../../pipe/dto/dto.pipe';
import { ResponseUtilService } from '@app/response-util';
import { Notice } from '@app/db/entity/Notice';

@Controller('notice')
export class NoticeController {
    constructor(
        private readonly service:NoticeService
    ){}

    @Post('page')
    async page(@Body(new DtoPipe({skipMissingProperties : true})) dto:PageDto) {
        //const memberId = req[ReqAttrs.MEMBER_ID]
        const result = await this.service.page(dto)

        return ResponseUtilService.successRes(result)

    }


    @Post('add')
    async add(@Body() dto:Notice) {
        try {
            const r = await this.service.add(dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('update')
    async update(@Body() dto:Notice) {
        try {
            const r = await this.service.update(dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('delete')
    async del(@Body('id', ParseIntPipe) id:number) {
        if(id < 1) return null
        try {
            const r = await this.service.del(id)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        } 
    }

    @Post('send')
    async send(@Body('id', ParseIntPipe) id:number) {
        
        try {
            await this.service.send(id)
            return ResponseUtilService.successRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        } 
    }
}
